package edu.bu.cs673.superkids123.children.view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import edu.bu.cs673.superkids123.AbstractFrame;
import edu.bu.cs673.superkids123.AbstractNavigationView;
import edu.bu.cs673.superkids123.children.DataObjectAccess.ReportManager;
import edu.bu.cs673.superkids123.children.DataObjectAccess.StatManager;
import edu.bu.cs673.superkids123.children.controller.AchievementController;
import edu.bu.cs673.superkids123.children.controller.ChildProfileController;
import edu.bu.cs673.superkids123.children.model.Theme;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResource;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResourceProvider;
import edu.bu.cs673.superkids123.common.model.Child;
import edu.bu.cs673.superkids123.common.model.AchievementRule;
import edu.bu.cs673.superkids123.parents.DataObjectAccess.ParentSystemManager;
import edu.bu.cs673.superkids123.parents.model.Image;

public class ChildProfileView extends AbstractNavigationView implements
		ActionListener {

	JPanel panel = new JPanel(new GridLayout(1, 2, H_GAP, V_GAP));

	ChildrenResourceProvider provider = ChildrenResourceProvider.getInstance();
	private static float AVATAR_SCALE = 0.75f;
	private JPanel left = new JPanel(new CardLayout());
	private static String AVATAR_CARD = "AVATAR_CARD";
	private static String AVATAR_SEL_CARD = "AVATAR_SEL_CARD";
	JPanel leftGrid = new JPanel(new GridLayout(0, 3));
	private JButton avatar = new JButton();
	JButton achievements = new JButton();
	ArrayList<JButton> profileImageButtons = new ArrayList<JButton>();

	private static int H_GAP = 20;
	private static int V_GAP = H_GAP;

	public ChildProfileView(AbstractFrame mainFrame) {
		super(mainFrame);
	}

	@Override
	protected JPanel layout() {
		Child child = (Child) authController.getCurrentUser();
		
		setAvatarVisible(false);
		setTitleBorderVisible(false);
		setParentsButtonVisible(false);
		
		panel.removeAll();
		left.removeAll();
		leftGrid = new JPanel(new GridLayout(0, 3));
		
		JPanel leftAvatar = new JPanel(new BorderLayout());
		avatar.setOpaque(false);
		avatar.setContentAreaFilled(false);
		avatar.setBorderPainted(false);
		avatar.addActionListener(this);
		avatar.setIcon(provider.getScaledImage(child.getImage().getFileName(), AVATAR_SCALE * 3));
		leftAvatar.add(avatar, BorderLayout.CENTER);
		left.add(leftAvatar, AVATAR_CARD);

		List<Image> avatars = ParentSystemManager.getImages();
		
		profileImageButtons.clear();
		Iterator<Image> i = avatars.iterator(); 
		while (i.hasNext()) {
			String imageResource = i.next().getFileName();
			if (!imageResource.equals(child.getImage().getFileName())) {
				JButton button =  new JButton(imageResource);
				button.setHideActionText(true);
				button.setBackground(Color.GRAY);
				button.addActionListener(this);
				button.setIcon(provider.getScaledImage(imageResource, AVATAR_SCALE));
				leftGrid.add(button);
				profileImageButtons.add(button);
			}
		}
		left.add(leftGrid, AVATAR_SEL_CARD);

		panel.add(left);
		
		JPanel right = new JPanel(new GridLayout(2, 1));
		JPanel info = new JPanel(new GridLayout(5, 2));
		TitledBorder infoBorder = BorderFactory.createTitledBorder(" Information ");
		info.setBorder(infoBorder);
		JLabel nameLabel = new JLabel("Name:");
		info.add(nameLabel);
		JLabel name = new JLabel(child.getName());
		info.add(name);
		JLabel ageLabel = new JLabel("Age:");
		info.add(ageLabel);
		JLabel age = new JLabel(Integer.toString(child.getAge()));
		info.add(age);
		JLabel lessonsLabel = new JLabel("Lessons learned:");
		info.add(lessonsLabel);
		JLabel lessons = new JLabel(Integer.toString(ReportManager.getAllCompletedLessons(child).size()));
		info.add(lessons);
		JLabel gamesLabel = new JLabel("Games played:");
		info.add(gamesLabel);
		JLabel games = new JLabel(Integer.toString(ReportManager.getAllCompletedGames(child).size()));
		info.add(games);
		int playTimeRemaining = authController.getPlayTimeRemaining();
		if (playTimeRemaining >= 0)
		{
			JLabel playTimeLabel = new JLabel("Remaining play time:");
			info.add(playTimeLabel);
			int min = (playTimeRemaining) % 60;
			int hr = playTimeRemaining / 60;
			String totalPlay = hr + " hours, " + min + " mins";
			JLabel playTime = new JLabel(totalPlay);
			info.add(playTime);
		}
		right.add(info);
		int points = getMainFrame().getController(AchievementController.class).getPoints();
		achievements.setText(Integer.toString(points) + " Achievement Points");
		achievements.setFont(Theme.CHILD_FONT);
		achievements.setContentAreaFilled(false);
		achievements.addActionListener(this);
		achievements.setIcon(provider.getScaledImage(ChildrenResource.imageStar, AVATAR_SCALE / 2));
		right.add(achievements);
		panel.add(right);

		return panel;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(avatar)) {	
		    CardLayout cl = (CardLayout)(left.getLayout());
		    cl.show(left, AVATAR_SEL_CARD);
		/* TODO move this to parent settings
		 * 		ChildrenResource.imageIconFolderOpen
		 * } else if (((JButton)e.getSource()).getText().equals(ChildrenResource.imageIconFolderOpen)) {
			JFileChooser fc = new JFileChooser();
			int n = fc.showOpenDialog(new JPanel());

			if (n == JFileChooser.APPROVE_OPTION) {
				File file = fc.getSelectedFile();
				JOptionPane.showConfirmDialog(new JPanel(),
						file.getAbsolutePath());
			}
		 */
		} else if (e.getSource().equals(achievements)) {
			getMainFrame().changeView(AchievementView.class);
		} else {
			JButton button = (JButton)e.getSource();
			String imageFileName = button.getText();
			Child child = (Child) authController.getCurrentUser();
			String oldFileName = child.getImage().getFileName();
			getMainFrame().getController(ChildProfileController.class).updateAvatar(imageFileName);
	
			avatar.setIcon(provider.getScaledImage(imageFileName, AVATAR_SCALE * 3));
		    CardLayout cl = (CardLayout)(left.getLayout());
		    cl.show(left, AVATAR_CARD);

			button.setIcon(provider.getScaledImage(oldFileName, AVATAR_SCALE));
			button.setText(oldFileName);
		}
	}

	@Override
	public String getTitle() {
		Child child = (Child) authController.getCurrentUser();
		
		panel.setBorder(new EmptyBorder(V_GAP, H_GAP, V_GAP, H_GAP));

		return child.getName().concat("'s Profile");
	}

	@Override
	public String getBorderTitle() {
		// no border used
		return null;
	}
}
